package com.ft.adbix.oaid.util;

import android.content.Context;

import com.ft.adbix.oaid.DeviceID;
import com.ft.adbix.oaid.IGetter;

public class OAIDUtils {
    private static boolean isSuccess = false;
    private static final int TIME_GET_OAID_TIME_OUT = 4000;

    public static String OAID = "";

    public static void loadOAID(Context context, final OnOAIDLoadCallBack callBack) {
        // 获取OAID/AAID，异步回调
        DeviceID.getOAID(context, new IGetter() {
            @Override
            public void onOAIDGetComplete(String result) {
                // 不同厂商的OAID/AAID格式是不一样的，可进行MD5、SHA1之类的哈希运算统一
                isSuccess = true;
                OAID = result;
                callBack.loadSuccess(result);
                OAIDLogUtil.d("调用工信部OAID SDK获取OAID="+result);
            }

            @Override
            public void onOAIDGetError(Exception error) {
                OAIDLogUtil.d("调用工信部OAID ERROR"+error.getMessage());

                // 获取OAID/AAID失败
                if (callBack != null) {
                    if (!isSuccess) {
                        callBack.loadFailed();
                    }
                }
            }
        });

//        if (Build.SDK_INT >= 29) {
//            MiitHelper miitHelper = new MiitHelper(ids -> {
//                isSuccess = true;
//                OAID = ids;
//                AndroidSchedulers.mainThread().scheduleDirect(() -> callBack.loadSuccess(ids));
//            });
//            miitHelper.getDeviceIds(context);
//            AndroidSchedulers.mainThread().scheduleDirect(() -> {
//                if (callBack != null) {
//                    if (!isSuccess) {
//                        callBack.loadFailed();
//                    }
//                }
//            }, TIME_GET_OAID_TIME_OUT, TimeUnit.MILLISECONDS);
//        } else {
//            if (callBack != null) {
//                callBack.loadFailed();
//            }
//        }

    }

    public interface OnOAIDLoadCallBack {
        void loadSuccess(String oaid);

        void loadFailed();
    }
}
